﻿using MediatR;
using W.Purchase.API.Application.Command.ReceivableCommand;
using W.Purchase.Repository.Interface;

namespace W.Purchase.API.Application.CommandHandler.ReceivableCommandHandler
{
    public class ReceivableAuditCommandHandler:IRequestHandler<ReceivableAuditCommand,int>
    {
        private readonly IReceivableRepository _receivableRepository;
        public ReceivableAuditCommandHandler(IReceivableRepository receivableRepository)
        {
            _receivableRepository = receivableRepository;
        }

        public async Task<int> Handle(ReceivableAuditCommand request, CancellationToken cancellationToken)
        {
            var list = await _receivableRepository.GetAllAsaync();
            var plist = list.Where(c => c.ReceivableId == request.ReceivableId).FirstOrDefault();
            if (plist != null)
            {
                plist.Status=request.Status;
            }
            return await _receivableRepository.Update(plist);
        }
    }
}
